using BlazorAdmin.Models;
using BootstrapBlazor.Components;
using System.ComponentModel;

namespace BlazorAdmin.Web.Shared.Models
{
    public class SettingsSearchModel : ITableSearchModel
    {
        [DisplayName("配置代码")]
        public string? Code { get; set; }

        [DisplayName("配置描述")]
        public string? Description { get; set; }

        [DisplayName("配置的值")]
        public string? Value { get; set; }

        [DisplayName("是否启用")]
        public bool? IsEnable { get; set; }

        [DisplayName("是否只读")]
        public bool? IsReadOnly { get; set; }

        [DisplayName("更新人")]
        public string? Updater { get; set; }

        [DisplayName("更新时间")]
        public DateTimeRangeValue? UpdateTime { get; set; }

        public SettingsSearchModel()
        {
            Reset();
        }

        public IEnumerable<IFilterAction> GetSearches()
        {
            var ret = new List<IFilterAction>();

            if (!string.IsNullOrEmpty(Code))
                ret.Add(new SearchFilterAction(nameof(Setting.Code), Code));

            if (!string.IsNullOrEmpty(Description))
                ret.Add(new SearchFilterAction(nameof(Setting.Description), Description));

            if (!string.IsNullOrEmpty(Value))
                ret.Add(new SearchFilterAction(nameof(Setting.Value), Value));

            if (IsEnable.HasValue)
                ret.Add(new SearchFilterAction(nameof(Setting.IsEnable), IsEnable, FilterAction.Equal));

            if (IsReadOnly.HasValue)
                ret.Add(new SearchFilterAction(nameof(Setting.IsReadOnly), IsReadOnly, FilterAction.Equal));

            if (!string.IsNullOrEmpty(Updater))
                ret.Add(new SearchFilterAction(nameof(Setting.Updater), Updater));

            if (UpdateTime != null)
            {
                ret.Add(new SearchFilterAction(nameof(Setting.UpdateTime), UpdateTime.Start, FilterAction.GreaterThanOrEqual));
                ret.Add(new SearchFilterAction(nameof(Setting.UpdateTime), UpdateTime.End, FilterAction.LessThanOrEqual));
            }

            return ret;
        }

        public void Reset()
        {
            Code = null;
            Description = null;
            Value = null;
            IsEnable = null;
            IsReadOnly = null;
            Updater = null;
            UpdateTime = null;
        }
    }
}
